package edu.hut.bookshop.service.impl;

import com.alipay.api.AlipayApiException;
import com.github.pagehelper.PageHelper;
import edu.hut.bookshop.Alipay.Alipay;
import edu.hut.bookshop.dao.AftersaleMapper;
import edu.hut.bookshop.dao.OrderItemMapper;
import edu.hut.bookshop.dao.OrderMapper;
import edu.hut.bookshop.dao.PayMapper;
import edu.hut.bookshop.pojo.Aftersale;
import edu.hut.bookshop.pojo.Order;
import edu.hut.bookshop.pojo.Pay;
import edu.hut.bookshop.service.PayService;
import edu.hut.bookshop.util.ResultCode;
import edu.hut.bookshop.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private PayMapper payMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private Alipay alipay;

    @Resource
    private AftersaleMapper aftersaleMapper;

    @Override
    public Integer insertPay(int orderId) {
        Pay pay = new Pay();
        pay.setOrderId(orderId);
        pay.setTotalAmount(orderItemMapper.findsumpriceByorderId(orderId));
        pay.setPstatus(0);//状态为未付款
        pay.setCreateTime(new Date());
        pay.setSn("Store"+UUID.randomUUID().toString().replace("-", "").toUpperCase());
        Integer rows = payMapper.insertPay(pay);
        return rows;
    }

    @Override
    public Pay findByorderId(int orderId) {
        Pay pay = payMapper.findByorderId(orderId);
        return pay;
    }

    @Override
    public List<Pay> findByuserId(int userId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        //先通过用户id查询订单的集合
        List<Integer> orders = orderMapper.selectorderIdsByUserId(userId);
        System.out.println(orders.size());
        //再根据订单id去查询用户的支付订单
        List<Pay> pays = new ArrayList<>();
        for(int i = 0;i<orders.size();i++){
            if(payMapper.findByorderId(orders.get(i)).getSn()!=null&&payMapper.findByorderId(orders.get(i))!=null) {
                System.out.println(orders.get(i));
                pays.add(payMapper.findByorderId(orders.get(i)));
            }
        }
        System.out.println("支付订单为"+pays.size());
        return pays;
    }

    @Override
    public String Alipay(Pay pay) throws AlipayApiException {
        return alipay.pay(pay.getSn(),pay.getTotalAmount(),"订单"+pay.getPayid()+"号");
    }

    @Override
    public Pay findBysn(String sn) {
        return payMapper.findBysn(sn);
    }

    @Override
    public Pay findBypaysn(String paysn) {
        return payMapper.findBypaysn(paysn);
    }

    @Override
    public Pay save(Pay pay) {
        return pay;
    }

    @Override
    public int updatePay(String sn, String paysn, Integer pstatus, Date payTime) {
        int rows = payMapper.updatePay(sn,paysn,pstatus,payTime);
        //支付成功，更改售后表
        Pay pay = payMapper.findBysn(sn);
        aftersaleMapper.updateAftersalebyorderId(pay.getOrderId(),1);
        return rows;
    }
}
